#include<iostream>
using namespace std;

#include<cmath>

double Deriv2( double x, double h, double (*f)(double) ){

	return ( f(x+h) + f(x-h) - 2*f(x) ) / h / h;
}

/*
 * int n:  number of known data points
 * double x: the interpolation/extrapolation point x coordinate
 */
double LagPoly( int n, double * xdata, double * ydata, double x ){

        double y=0, z;
        for(int i=0;i<n;i++){
                z = 1;
                for(int k=0;k<n;k++){
                        if( k != i ) z *= (x - xdata[k] )/ ( xdata[i] - xdata[k] );
                }
                y += ydata[i] * z;
        }
        return y;
}


int main(){

        int n;
        //cout<<" number of known data points n = "; cin>>n;

	for(int n=1;n<=20;n++){
		double * xdata = new double [n];
		double * ydata = new double [n];

		double h = 0.1 / n;

		//cout<<"Data:"<<endl;
		for(int i=0;i<n;i++){
			xdata[i] = (i+1)*h;
			ydata[i] = Deriv2( 1, xdata[i], exp );
			//cout<<xdata[i]<<"\t"<<ydata[i]<<endl;
		}

		cout<<" n= "<<n<<"\t error = "<< LagPoly( n, xdata, ydata, 0 ) - M_E <<endl;

		delete [] xdata; delete [] ydata;
	}

	return 0;
}
